package Test02;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class BankSystem {
    private List<BankCard> cards;

    public BankSystem() {
        this.cards = new ArrayList<>();
    }

    public void addCard(BankCard card) {
        cards.add(card);
    }

    public void removeCard(String cardNumber) {
        // 通过卡号删除银行卡...
        //提示利用removeIf函数
        cards.removeIf(card -> card.getCardNumber().equals(cardNumber));
    }

    public BankCard findCardByNumber(String cardNumber) {
        // 通过卡号查找银行卡...
        for (BankCard card : cards) {
            if (card.getCardNumber().equals(cardNumber)) {
                return card;
            }
        }
        return null;
    }


    public void displayAllCards() {
        // 打印所有银行卡信息...
        for (BankCard card : cards) {
            System.out.println(card);
        }
    }


    public void calculateTotalExpenses(String cardNumber) {
        // 计算指定卡的总花费
        //提示：所有的花费对应的交易类型为“支出”， 利用findCardByNumber函数根据卡号查找对于银行卡，并计算其所有交易记录中的花费总合，并输出计算结果。
        BankCard card = findCardByNumber(cardNumber);
        if (card != null) {
            double totalExpenses = 0;
            for (Transaction transaction : card.getTransactions()) {
                if (transaction.getType().equals("支出")) {
                    totalExpenses += transaction.getAmount();
                }
            }
            System.out.println("卡号：" + cardNumber + "的总花费为：" + totalExpenses);
        }
    }

    public List<BankCard> getCards() {
        return cards;
    }//获取系统中的所有银行卡

    public static void main(String[] args) {//参考测试用例
// 添加银行卡、删除银行卡、根据卡号查询银行卡、显示所有银行卡信息、计算并显示每张卡的总消费金额
        BankSystem bs = new BankSystem();
//新建银行卡
        BankCard card1 = new BankCard("001", "zhangsan");
        BankCard card2 = new BankCard("002", "lisi");
        BankCard card3 = new BankCard("003", "wanger");
//添加银行卡
        bs.addCard(card1);
        bs.addCard(card2);
        bs.addCard(card3);
//设置银行卡交易记录
        card1.setTransactions(new Transaction(1000, "收入"));
        card1.setTransactions(new Transaction(100, "支出"));
        card1.setTransactions(new Transaction(50, "支出"));

        card2.setTransactions(new Transaction(1000, "收入"));

        card3.setTransactions(new Transaction(1000, "收入"));
        card3.setTransactions(new Transaction(800, "支出"));
//显示所有银行卡信息,计算并显示每张卡的总消费金额
        bs.displayAllCards();
        for (BankCard bc : bs.getCards()) {
            bs.calculateTotalExpenses(bc.getCardNumber());
        }
        System.out.println("请输入需要删除的银行卡卡号：");
        Scanner sc = new Scanner(System.in);
        String cardNumber = sc.next();
        bs.removeCard(cardNumber);
        bs.displayAllCards();
    }

}
